<template>
  <div class="system-edit-role-container">
    <el-dialog :title="'余额调整'" v-model="isShowDialog" width="769px">
      <el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="90px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="余额调整" prop="name">
              <el-input v-model="formData.amount" placeholder="请输入金额" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="资金来源">
              <el-input v-model="formData.direction" type="textarea" placeholder="请输入该资金来源" maxlength="150"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="备注">
              <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注信息" maxlength="150"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="类型">
              <el-input v-model="formData.type" type="textarea" placeholder="请输入该资金类型(美元，人民币)" maxlength="150"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form> 
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">重置</el-button>
          <el-button type="primary" @click="onSubmit" size="default" :loading="loading">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>


<script lang="ts">
import { reactive, toRefs, defineComponent, ref, getCurrentInstance } from 'vue';
import {getRoleParams } from "/@/api/system/role";
import { ElMessage } from "element-plus";
import { refreshBackEndControlRoutes } from "/@/router/backEnd";
import {addadjust } from '/@/api/MemberManagement/MemberList';

// 定义接口来定义对象的类型
interface MenuDataTree {
  id: number;
  pid: number;
  title: string;
  children?: MenuDataTree[];
}
interface DialogRow {
  amount: string,
  direction: string,
  type: string,
  remark: string,
  userId: string
}
interface RoleState {
  loading: boolean;
  isShowDialog: boolean;
  formData: DialogRow;
  menuData: Array<MenuDataTree>;
  menuExpand: boolean;
  menuNodeAll: boolean;
  menuCheckStrictly: boolean;
  menuProps: {
    children: string;
    label: string;
  };
  rules: object;
}

export default defineComponent({
  name: 'systemEditRole',
  props: {
		// 定义message为字符串类型
		ids: String as () => string,
	},
  setup(props, { emit }) {
    const { proxy } = getCurrentInstance() as any;
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();
    const state = reactive<RoleState>({
      loading: false,
      isShowDialog: false,
      formData: {
        amount: '',
        direction: '',
        type: '',
        remark: '',
        userId: ''
      },
      // 表单校验
      rules: {
      
      },
      menuData: [],
      menuExpand: false,
      menuNodeAll: false,
      menuCheckStrictly: false,
      menuProps: {
        children: 'children',
        label: 'title',
      },
    });
    // 打开弹窗
    const openDialoga = (row?: DialogRow) => {
      state.formData.userId =  String(props.ids) 
      resetForm();
      getMenuData();
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
    };
    // 新增
    const onSubmit = () => {
           //添加
           state.formData.userId =  String(props.ids) 
           addadjust(state.formData).then(() => {
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              resetMenuSession()
              emit('getRoleList')
            }).finally(() => {
              state.loading = false;
            })
    };
    // 获取菜单结构数据
    const getMenuData = () => {
      getRoleParams().then((res: any) => {
        state.menuData = proxy.handleTree(res.data.menu, "id", "pid");
      })
    };
    const resetForm = () => {
      state.menuCheckStrictly = false;
      state.menuExpand = false;
      state.menuNodeAll = false;
      state.formData = {
        amount: '',
        direction: '',
        type: '',
        remark: '',
        userId: ''
      }
    };
    // 重置菜单session
    const resetMenuSession = () => {
      refreshBackEndControlRoutes();
    };
    return {
      openDialoga,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,
      resetMenuSession,
      ...toRefs(state),
    };
  },
});
</script>

<style scoped lang="scss">
.tree-border {
  margin-top: 5px;
  border: 1px solid #e5e6e7 !important;
  border-radius: 4px;
}

.system-edit-role-container {
  .menu-data-tree {
    border: var(--el-input-border, var(--el-border-base));
    border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
    padding: 5px;
  }
}
</style>
